<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/9
 * Time: 14:41
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Privateskin extends Base
{
    /**
     * 皮肤首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['pskin_status'] = 1;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('private_skin')->where($where)->count(); // 获取总条数
            $lists = Db::name('private_skin')
                ->where($where)
                ->field('pskin_id, pskin_name, pskin_time')
                ->page($NowPage, $limits)
                ->order('pskin_time desc')
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加皮肤配置
     * @return mixed|\think\response\Json
     */
    public function skinAdd(){
        if(Request::instance()->isPost()){
            $pskin_name = trim(input('pskin_name'));
            $insert = [
                'pskin_name' => $pskin_name,
                'pskin_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_skin')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('privateskin/skinadd');
    }
    
    /**
     * 编辑皮肤配置
     * @return mixed|\think\response\Json
     */
    public function skinEdit(){
        $pskin_id = input('pskin_id');
        if(Request::instance()->isPost()){
            $pskin_name = trim(input('pskin_name'));
            $update = [
                'pskin_name' => $pskin_name,
                'pskin_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_skin')->where('pskin_id', $pskin_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $skin = Db::table('up_private_skin')->where('pskin_id', $pskin_id)->field('pskin_name')->find();
        $this->assign([
            'pskin_id' => $pskin_id,
            'skin' => $skin
        ]);
        return $this->fetch('privateskin/skinedit');
    }
    
    /**
     * 删除皮肤配置
     * @return \think\response\Json
     */
    public function skinDel(){
        $pskin_id = input('id');
        $res = Db::table('up_private_skin')->where('pskin_id', $pskin_id)->setField('pskin_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 配置选项列表
     * @return mixed|\think\response\Json
     */
    public function configOption(){
        if(request()->isAjax()){
            $where['psc_status'] = 1;
            $where['psc_pskin_id'] = session('pskin_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('private_skin_config')->where($where)->count();// 获取总条数
            $lists = Db::name('private_skin_config')
                ->where($where)
                ->field('psc_id, psc_name, psc_time')
                ->page($NowPage, $limits)
                ->order('psc_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $pskin_id = input('pskin_id');
        if(isset($pskin_id) && $pskin_id) session('pskin_id', $pskin_id);
        return $this->fetch('privateskin/configoption');
    }
    
    /**
     * 添加配置选项
     * @return mixed|\think\response\Json
     */
    public function configAdd(){
        if(Request::instance()->isPost()){
            $psc_name = trim(input('psc_name'));
            $insert = [
                'psc_pskin_id' => session('pskin_id'),
                'psc_name' => $psc_name,
                'psc_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_skin_config')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('privateskin/configadd');
    }
    
    /**
     * 编辑配置选项
     * @return mixed|\think\response\Json
     */
    public function configEdit(){
        $psc_id = input('psc_id');
        if(Request::instance()->isPost()){
            $psc_name = trim(input('psc_name'));
            $update = [
                'psc_id' => $psc_id,
                'psc_name' => $psc_name,
                'psc_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_skin_config')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $config = Db::table('up_private_skin_config')->where('psc_id', $psc_id)->field('psc_name')->find();
        $this->assign([
            'psc_id' => $psc_id,
            'config' => $config
        ]);
        return $this->fetch('privateskin/configedit');
    }
    
    /**
     * 删除皮肤配置项
     * @return \think\response\Json
     */
    public function configDel(){
        $psc_id = input('id');
        $res = Db::table('up_private_skin_config')
            ->where('psc_id', $psc_id)
            ->setField('psc_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 200, 'msg' => '删除失败']);
        }
    }
}